﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDD;
using Xunit;

namespace DokiDokiTest {
    public class TestFrameBuffer {
        [Fact]
        public void Test_New () {
            var fbuf = new FrameBuffer (2, 1, 1);

            Assert.Equal (2, fbuf.ColorBufferCount);
            Assert.Equal (0, fbuf.ColorBuffers.Count ());
            Assert.Equal (1, fbuf.DepthBufferCount);
            Assert.Equal (0, fbuf.DepthBuffers.Count ());
            Assert.Equal (null, fbuf.GetColorBuffer (0));
            Assert.Equal (null, fbuf.GetColorBuffer (1));
            Assert.Equal (null, fbuf.GetDepthBuffer ());
            Assert.Equal (null, fbuf.ColorBuffer);
            Assert.Equal (null, fbuf.DepthBuffer);
        }

        [Fact]
        public void Test_SetColorBuffer () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var tex1 = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var tex2 = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);

            fbuf.SetColorBuffer (0, tex1);
            fbuf.SetColorBuffer (1, tex2);
            Assert.Equal (2, fbuf.ColorBufferCount);
            Assert.Equal (2, fbuf.ColorBuffers.Count ());
            Assert.Equal (tex1, fbuf.ColorBuffer);
            Assert.Equal (tex1, fbuf.GetColorBuffer (0));
            Assert.Equal (tex2, fbuf.GetColorBuffer (1));

        }

        [Fact]
        public void Test_SetDepthBuffer () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var tex = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            fbuf.SetDepthBuffer (tex);

            Assert.Equal (1, fbuf.DepthBufferCount);
            Assert.Equal (tex, fbuf.DepthBuffer);
            Assert.Equal (tex, fbuf.GetDepthBuffer());
        }

        [Fact]
        public void Test_References () {
            var img1 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img2 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img3 = new Image2D<float> (PixelFormat.Depth, 16, 16);
           
            var fbuf = new FrameBuffer (2, 1, 1);
            fbuf.SetColorBuffer (0, img1);
            fbuf.SetColorBuffer (1, img2);
            fbuf.SetDepthBuffer (img3);

            var objs = fbuf.References;

            Assert.Equal (3, objs.Count ());
            Assert.Equal (true, objs.All (x => x != null));
        }

        [Fact]
        public void Test_Find () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var img1 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img2 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img3 = new Image2D<byte> (PixelFormat.Depth, 16, 16);
            fbuf.SetColorBuffer (0, img1);
            fbuf.SetColorBuffer (1, img2);
            fbuf.SetDepthBuffer (img3);

            img1.UserID = 101;
            img2.UserID = 102;
            img3.UserID = 103;
            fbuf.UserID = 104;

            Assert.Equal (img1, fbuf.Find (101));
            Assert.Equal (img2, fbuf.Find (102));
            Assert.Equal (img3, fbuf.Find (103));
            Assert.Equal (fbuf, fbuf.Find (104));
        }
    }
}
